package com.nanhu.test.pop;

import java.util.ArrayList;
import com.nanhu.test.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.CheckedTextView;
import android.widget.FrameLayout;
import android.widget.TextView;

public class PopMenuActivity extends Activity{
	
	private static final int ID_POPUP_MENU_A = 0x1111;

    private static final int ID_POPUP_MENU_B = 0x1112;

    private static final int ID_POPUP_MENU_C = 0x1113;

    private static final int ID_POPUP_MENU_D = 0x1114;

    private static final int ID_POPUP_MENU_E = 0x1115;

    private static final int MAX_POPUP_MENU_ITEMS = 5;

    private static final ArrayList<MenuItem> sItems = new ArrayList<MenuItem>();
    static {
        sItems.add(new MenuItem(1, ID_POPUP_MENU_A, R.drawable.stat_notify_alarm, "Alarm"));
        sItems.add(new MenuItem(2, ID_POPUP_MENU_B, R.drawable.stat_notify_car_mode, "Car Mode"));
        sItems.add(new MenuItem(3, ID_POPUP_MENU_C, R.drawable.stat_notify_chat, "Chat"));
        sItems.add(new MenuItem(4, ID_POPUP_MENU_D, R.drawable.stat_notify_sim_toolkit, "Sim Toolkit"));
        sItems.add(new MenuItem(5, ID_POPUP_MENU_E, R.drawable.stat_notify_sync, "Sync"));
    }
	
	public static class MenuItem {

        public int position;

        public int id;

        public String text;

        public int textID;

        public int iconID;

        public MenuItem(int position, int id, int iconID, String text) {
            this.position = position;
            this.id = id;
            this.iconID = iconID;
            this.text = text;
        }

        public MenuItem(int position, int id, int iconID, int textID) {
            this.position = position;
            this.id = id;
            this.iconID = iconID;
            this.textID = textID;
        }
    }

    private static final int MSG_SHOW_POPUP_MENU = 0x12100;

    private static final int MSG_HIDE_POPUP_MENU = 0x12120;

    private ViewGroup mMenuContent = null;

    private boolean mIsDestoryed = false;

    private Animation mMenuSwitchOpenAnim = null;

    private Animation mMenuSwitchCloseAnim = null;

    private Animation mMenuContentOpenAnim = null;

    private Animation mMenuContentCloseAnim = null;

    private boolean mItemClickBlock = false;

    private final Handler mHandler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            if (mIsDestoryed) {
                removeMessages(MSG_HIDE_POPUP_MENU);
                removeMessages(MSG_SHOW_POPUP_MENU);
                return;
            }

            switch (msg.what) {
                case MSG_SHOW_POPUP_MENU:
                    removeMessages(MSG_HIDE_POPUP_MENU);
                    showPopupMenu();
                    break;
                case MSG_HIDE_POPUP_MENU:
                    removeMessages(MSG_SHOW_POPUP_MENU);
                    hidePopupMenu();
                    break;
                default:
                    super.handleMessage(msg);
                    break;
            }
        };
    };

    private final OnClickListener mMenuItemClickListener = new OnClickListener() {

        @Override
        public void onClick(View item) {
            if(mItemClickBlock){
                return;
            }
            switch (item.getId()) {
                case R.id.check:
                    openOrClosePopupMenu(item);
                    break;
                default:
                    performPopupMenuItemClick(item);
                    break;
            }
        }
    };

    private final AnimationListener mAnimationListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            if (animation.equals(mMenuSwitchOpenAnim)) {
                mItemClickBlock = true;
                mMenuContent.startAnimation(mMenuContentCloseAnim);
            } else if (animation.equals(mMenuSwitchCloseAnim)) {
                mItemClickBlock = true;
                mMenuContent.startAnimation(mMenuContentOpenAnim);
            } else if (animation.equals(mMenuContentOpenAnim)) {
                mMenuContent.setVisibility(View.VISIBLE);
            } else if (animation.equals(mMenuContentCloseAnim)) {
            }

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (animation.equals(mMenuSwitchOpenAnim)) {
                final CheckedTextView check = (CheckedTextView) findViewById(R.id.check);
                check.setVisibility(View.GONE);
                final Handler handler = mHandler;
                handler.sendEmptyMessage(MSG_HIDE_POPUP_MENU);
            } else if (animation.equals(mMenuSwitchCloseAnim)) {
                final CheckedTextView check = (CheckedTextView) findViewById(R.id.check);
                check.setVisibility(View.GONE);
                final Handler handler = mHandler;
                handler.sendEmptyMessage(MSG_SHOW_POPUP_MENU);
            } else if (animation.equals(mMenuContentOpenAnim)) {
                mItemClickBlock = false;
            } else if (animation.equals(mMenuContentCloseAnim)) {
                mMenuContent.setVisibility(View.GONE);
                mItemClickBlock = false;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_abs_popup_menu);

        final OnClickListener clickListener = mMenuItemClickListener;
        findViewById(R.id.check).setOnClickListener(clickListener);
        final ViewGroup menuContent = (ViewGroup) findViewById(R.id.popup_menu_content);
        menuContent.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        createPopupMenuItem(menuContent, clickListener);
        mMenuContent = menuContent;

        final Context context = getApplicationContext();
        final AnimationListener animationListener = mAnimationListener;
        mMenuSwitchOpenAnim = AnimationUtils.loadAnimation(context,
                R.anim.popup_menu_switch_open_rotate);
        mMenuSwitchCloseAnim = AnimationUtils.loadAnimation(context,
                R.anim.popup_menu_switch_close_rotate);
        mMenuContentOpenAnim = AnimationUtils.loadAnimation(context,
                R.anim.popup_menu_content_open_scale_alpha);
        mMenuContentCloseAnim = AnimationUtils.loadAnimation(context,
                R.anim.popup_menu_content_close_scale_alpha);

        mMenuSwitchOpenAnim.setAnimationListener(animationListener);
        mMenuSwitchCloseAnim.setAnimationListener(animationListener);
        mMenuContentOpenAnim.setAnimationListener(animationListener);
        mMenuContentCloseAnim.setAnimationListener(animationListener);

    }

    protected void performPopupMenuItemClick(View item) {
        if (!mItemClickBlock) {
            final CheckedTextView check = (CheckedTextView) findViewById(R.id.check);
            if (check.isChecked()) {
                openOrClosePopupMenu(check);
            }
            // perform PopupMenu Item Click
            onPopupMenuItemClicked(item, (Integer) item.getTag(), item.getId());
        }
    }

    private void openOrClosePopupMenu(View item) {
        final CheckedTextView check = (CheckedTextView) item;
        final boolean checked = check.isChecked();
        if (checked) {
            item.startAnimation(mMenuSwitchOpenAnim);
        } else {
            item.startAnimation(mMenuSwitchCloseAnim);
        }
    }

    protected void hidePopupMenu() {
        // hide PopupMenu
        final CheckedTextView check = (CheckedTextView) findViewById(R.id.check);
        check.setChecked(!check.isChecked());
        check.setVisibility(View.VISIBLE);
    }

    protected void showPopupMenu() {
        // show PopupMenu

        final CheckedTextView check = (CheckedTextView) findViewById(R.id.check);
        check.setChecked(!check.isChecked());
        check.setVisibility(View.VISIBLE);
    }

    protected void createPopupMenuItem(ViewGroup parent, OnClickListener clickListener){
    	final LayoutInflater layoutInflater = getLayoutInflater();
        for (int i = 0; i < MAX_POPUP_MENU_ITEMS; i++) {
            if (i > 0 && i <= MAX_POPUP_MENU_ITEMS - 1) {
                final View line = layoutInflater.inflate(R.layout.item_popup_menu_item_line,
                        parent, false);
                parent.addView(line);
            }
            final TextView item = (TextView) layoutInflater.inflate(R.layout.item_popup_menu_item,
                    parent, false);
            item.setOnClickListener(clickListener);

            final MenuItem menuItem = sItems.get(i);
            item.setId(menuItem.id);
            item.setTag(menuItem.position);
            if (!TextUtils.isEmpty(menuItem.text)) {
                item.setText(menuItem.text);
            } else if (menuItem.textID != 0) {
                item.setText(menuItem.textID);
            }
            if (menuItem.iconID != 0) {
                item.setCompoundDrawablesWithIntrinsicBounds(menuItem.iconID, 0, 0, 0);
            }
            parent.addView(item);
        }
    }

    protected void onPopupMenuItemClicked(View view, int position, int id){
    	Log.d("PopopMenu", "onPopupMenuItemClicked -- view = " + view + "  position = " + position + " id = " + id);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (!mItemClickBlock) {
                    final CheckedTextView check = (CheckedTextView) findViewById(R.id.check);
                    if (check.isChecked()) {
                        openOrClosePopupMenu(check);
                    }
                }
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_MENU:
                if (!mItemClickBlock) {
                    final CheckedTextView check = (CheckedTextView) findViewById(R.id.check);
                    openOrClosePopupMenu(check);
                }
                return true;
            default:
                return super.onKeyUp(keyCode, event);
        }
    }


    @Override
    protected void onDestroy() {
        mIsDestoryed = true;
        mMenuContent = null;

        if (mMenuSwitchOpenAnim != null) {
            mMenuSwitchOpenAnim.setAnimationListener(null);
            mMenuSwitchOpenAnim = null;
        }
        if (mMenuSwitchCloseAnim != null) {
            mMenuSwitchCloseAnim.setAnimationListener(null);
            mMenuSwitchCloseAnim = null;
        }
        super.onDestroy();
    }
}
